from dao.user.userDao import UserDao
from flask import jsonify
from utils.token import certify_token, generate_token

user_dao = UserDao()


def login(username, password):
    user = user_dao.select_user_by_username(username=username)
    if user is None:
        return jsonify(code=1, msg='用户不存在')
    if user[2] != password:
        return jsonify(code=2, msg='密码错误')
    token = generate_token(username)
    return jsonify(code=0, msg='登录成功', user_name=user[1], token=token)


def info(username):
    user = user_dao.select_user_by_username(username=username)
    if user is None:
        return jsonify(code=1, msg='用户不存在')
    return jsonify(code=0, msg='查询成功', user=user)